{% extends "lava_scheduler_app/_content.html" %}

{% load django_tables2 %}
{% load static %}

{% block content %}
{% if username != request.user.username %}
<h2>Favorite jobs by {{ username }}</h2>
{% else %}
<h2>My favorite jobs</h2>
{% endif %}
<p><a href="#" data-toggle="modal" data-target="#user_select_modal">Favorite jobs by user</a></p>

{% render_table favoritejobs_table %}

<!-- Modal HTML -->
<div id="user_select_modal" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">Select user</h4>
      </div>
      <form class="well" id="user_select_form" method="post" action="{% url 'lava.scheduler.favorite_jobs' %}">
        <div class="modal-body">
	  {% csrf_token %}
          <div style="font-size: 11px;">Type to search for the system user.<br>Available users will appear in a list below.
          </div>
          <div style="display: inline;">
            <label for="username">Username:</label>
          </div>
          <div class="typeahead__container">
            <div class="typeahead__field">
              <div class="typeahead__query">
                <input id="username" name="username" type="search" placeholder="Search" autocomplete="off" />
              </div>
            </div>
          </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary">Save</button>
      </div>
      </form>
    </div>
  </div>
</div>

{% endblock %}

{% block scripts %}
<script type="text/javascript" src="{% static "lava_server/js/jquery.typeahead.js" %}"></script>
<script>
  $("#user_select_modal").on('show.bs.modal', function (e) {
    $("#username").typeahead({
      source: {users: {ajax: {type:"GET", url: '{% url 'lava.scheduler.username_list_json' %}', data: {term: $(this).val()}}}},
      display: ["name"],
      minLength: 1,
    });
  });
</script>
{% endblock %}
